package cn.orange.ch01_array.slidewindow;

/**
 * LC209.长度最小的子数组
 */
public class LC209 {
    public int minSubArrayLen(int target, int[] nums) {
        int minLen = nums.length + 1;
        int left = 0;
        int right = 0;
        int sum = 0;
        while (right < nums.length) {
            sum += nums[right];
            while (sum >= target) {
                minLen = Math.min(minLen, right - left + 1);
                sum -= nums[left++];
            }
            right++;
        }
        return minLen > nums.length ? 0 : minLen;
    }

    public static void main(String[] args) {
        LC209 alg = new LC209();
        System.out.println(alg.minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3}));
        System.out.println(alg.minSubArrayLen(4, new int[]{1, 4, 4}));
        System.out.println(alg.minSubArrayLen(11, new int[]{1, 1, 1, 1, 1, 1, 1, 1}));
    }
}
